var app = getApp()
var req = require('../../utils/reqUtils.js')
Page({
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    if(userInfo==""||userInfo==null){
    wx.navigateTo({
      url: "/pages/auth/login/login"
    });
  }
    // console.log("123456")
    var that=this
    // console.log("options :"+options)
    console.log("options.queryBean :"+options.queryBean)
    var queryBean = JSON.parse(options.queryBean);
    // console.log("queryBean: "+queryBean)
    console.log("queryBean.team_left_name: "+queryBean.team_left_name)
    that.setData({
      ['vote.gameId']:queryBean.id,
      ['winner[0].value']:queryBean.team_left_name,
      ['winner[1].value']:queryBean.team_right_name
    })
    //console.log(that.data.queryBean)
  },
  /**
   * 页面的初始数据
   */
  data: {
    winner:[
      {name:'1',value:''},
      {name:'2',value:''}
    ],
    gameNum:[
      {name:'1',value:'4:0'},
      {name:'2',value:'4:1'},
      {name:'3',value:'4:2'},
      {name:'4',value:'4:3'}
    ],
    winnerValue:{name:"",value:""},
    gameNumValue:{name:"",value:""},
    bothsides:{left:"",right:"",tempCurVal:""},
    vote:{userId:"",voter:"",gameId:"",winner:"",scoreId:""},
    information:[],
    modalHidden:true
  },
  //单选按钮发生变化
  radioChange1(e){
    console.log("e.detail.value1: "+e.detail.value);
    // console.log("winnerValue.name: "+this.data.winnerValue.name);
    // console.log("winnerValue.value: "+this.data.winnerValue.value);
    // winner.value=e.detail.value;
    if(e.detail.value=="1"){
      // winner.name="武汉EstartPro"
      this.setData({
        ['winnerValue.name']:this.data.winner[0].value,
        ['vote.winner']:this.data.winner[0].value,
        ['bothsides.left']:this.data.winner[0].value,
        ['bothsides.right']:this.data.winner[1].value
      })
      console.log("winnerValue.namees:  --"+this.data.winnerValue.name);
    }
    else if(e.detail.value=="2"){
      // winner.name="重庆狼队"  
      this.setData({
        ['winnerValue.name']:this.data.winner[1].value,
        ['vote.winner']:this.data.winner[1].value,
        ['bothsides.left']:this.data.winner[1].value,
        ['bothsides.right']:this.data.winner[0].value,
      })
      console.log("winnerValue.name :  --"+this.data.winnerValue.name);
    }
  },
    //单选按钮发生变化
    radioChange2(e){
      console.log("e.detail.value2: "+e.detail.value);
      let tempCurVal = '';
      var val=e.detail.value;
      switch (val) {
        case "1": tempCurVal = '4:0'; break
        case "2": tempCurVal = '4:1'; break
        case "3": tempCurVal = '4:2'; break
        case "4": tempCurVal = '4:3'; break
        default:
          tempCurVal = '打平'
          break
      };
      this.setData({
        // ['gameNumValue.name']:this.data.bothsides.left+tempCurVal+this.data.bothsides.right,
        ['vote.scoreId']:val,
        ['bothsides.tempCurVal']:tempCurVal
      })

    },
 







  //表单提交
  formSubmit(e){
    // console.log(e.detail.value);
    this.setData({
      ['gameNumValue.name']:this.data.bothsides.left+this.data.bothsides.tempCurVal+this.data.bothsides.right,
      ['winnerValue.name']:this.data.winnerValue.name,
      modalHidden:false
    })
  },
 

  
  //模态框取消
  modalCancel(){
    wx.showToast({
      title: '取消提交',
      icon:'none'
    })
    this.setData({
      modalHidden:true,
    })
  },
  //模态框确定
  modalConfirm:function(e) {
    let userInfo = wx.getStorageSync('userInfo');
    console.log("表单提交userInfo :"+JSON.stringify(userInfo))
    this.setData({
      ['vote.userId']:userInfo.userId,
      ['vote.voter']:userInfo.nickName,
      modalHidden: true
    })
    console.log("this.data.vote: "+JSON.stringify(this.data.vote))
    req.post('/game/insertVotes', this.data.vote, 0)
    .then(res => {
      if (res == ""||res == " ") {
        wx.showToast({
          title: '投票失败！！！',
          icon: 'loading',
          duration: 1500
        })
      } else {
        wx.showToast({
          title: '投票成功！！！',//这里打印出登录成功
          icon: 'success',
          duration: 1000
        })
    }
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
    show:'There are many endeavors to bring pleasure to people. Some places are making something you take inside. Not the food, but the experience. '
    })
    wx.navigateBack({
    delta: 1
    })
  
    })
  },

})

